package cn.dglydrpy.eshop.website.controller;


import cn.dglydrpy.eshop.api.IUserService;
import cn.dglydrpy.eshop.dal.entity.User;
import cn.dglydrpy.eshop.website.dto.UserDto;
import cn.dglydrpy.eshop.website.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dglydrpy
 * @since 2019-03-13
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;

    @PostMapping("/regist")
    public ResponseResult regist(UserDto userDto){
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        try{
            boolean flag = userService.save(user);
            return ResponseResult.create(flag);
        }catch(Exception e){
            e.printStackTrace();
            log.error("插入新用户["+userDto.getName()+"]失败,原因为: "+e.getMessage());
            return ResponseResult.create(e.getMessage());
        }
    }
}
